import qiniu from 'qiniu';
import { Request, Response } from 'express';
import logger from '../utils/logger'
const accessKey = 'Sf9Sv7rgAPn8mDHfSIMFejQGJtfiJkZ3qStfFWJt';
const secretKey = 'OHOlSlzYtehtFBrKriqJra2_sMq2pWT-7iXICFmW';
const bucket = 'zxz1992'
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
export class SourcesController {
  getUploadToken = async (req: Request, res: Response): Promise<void> => {
    try {
      const options = {
        scope: bucket,
        expires: 300, // 300s
      };
      const putPolicy = new qiniu.rs.PutPolicy(options);
      const uploadToken = putPolicy.uploadToken(mac);
      res.status(200).json({ code: 200, data: { uploadToken}, message: '获取上传凭证成功' });
    } catch (error) {
      logger.error(error)
      res.status(500).json({ message: '服务器错误' });
    }
  };
}